package cn.wit.summit.chwan.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.adapters.MakeMoneyRankingAdapter;
import cn.wit.summit.chwan.models.response.ResponseBase;
import cn.wit.summit.chwan.models.response.YesterdayIncomeRankingResponse;
import cn.wit.summit.chwan.retrofit.ApiUtil;
import cn.wit.summit.chwan.retrofit.MyCallback;
import cn.wit.summit.chwan.retrofit.RetrofitManager;
import cn.wit.summit.chwan.ui.base.BaseFragment;
import cn.wit.summit.chwan.utils.AccountUtil;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.LogUtils;
import cn.wit.summit.chwan.utils.NetWorkUtils;
import retrofit2.Retrofit;

/**
 * Created by Alan on 2018/7/25.
 */

public class MakeMoneyRankingFragment extends BaseFragment {

    @Bind(R.id.makeMoneyIncomeRankRv)
    RecyclerView makeMoneyIncomeRankRv;
    @Bind(R.id.failedMessageTv)
    TextView failedMessageTv;
    @Bind(R.id.root_content_null_ll)
    FrameLayout rootContentNullLl;

    private MakeMoneyRankingAdapter adapter;
    private List<MakeMoneyRankingAdapter.ViewBean> datas = new ArrayList<>();

    private RetrofitManager retrofitManager;
    private Retrofit retrofitLogin;
    private ApiUtil apiLogin;

    private int statusId = 1;//0:正常、1：无数据、2：未登录、3：无网络
    private int type = 1;//1：收入榜、2：分享棒、3：贡献榜

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_make_money_ranking, container, false);
        ButterKnife.bind(this, view);

        type = getArguments().getInt("type", 1);

        retrofitManager = RetrofitManager.getInstance(getActivity());
        retrofitLogin = retrofitManager.getLoginRetrofit();
        apiLogin = retrofitLogin.create(ApiUtil.class);

        initListView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtils.e("tbl>>>Ranking>>onResume()");
        if (statusId !=0) {
            getMoneyRanking();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogUtils.e("tbl>>>Ranking>>setUserVisibleHint");
        if (getActivity() != null && isVisibleToUser && statusId !=0) {
            getMoneyRanking();
        }
    }

    private void getMoneyRanking() {
        statusId = 0;

        int uid = 0;
        if (AccountUtil.getInstance().getAccountData() != null) {
            uid = AccountUtil.getInstance().getUid();
        }

        if (NetWorkUtils.isNetworkConnected(getActivity())) {
            apiLogin.getMakeMoneyRanking(uid, AccountUtil.getInstance().getToken())
                    .enqueue(new MyCallback<ResponseBase<YesterdayIncomeRankingResponse>>() {
                        @Override
                        public void onSuccess(ResponseBase<YesterdayIncomeRankingResponse> model) {
                            YesterdayIncomeRankingResponse data = model.getData_info();
                            upUiView(data);
                        }

                        @Override
                        public void onFailure(int code, String msg) {
                            showToast(msg);
                            if (code==-3) {
                                changeStatusView(3);
                            } else {
                                changeStatusView(1);
                            }
                        }
                    });
        } else {
            changeStatusView(3);
        }
    }

    private void changeStatusView(int m) {
        statusId = m;
        if (statusId == 0) {
            failedMessageTv.setVisibility(View.GONE);
            makeMoneyIncomeRankRv.setVisibility(View.VISIBLE);
            rootContentNullLl.setVisibility(View.GONE);
        } else if (statusId == 1) {
            failedMessageTv.setText(getActivity().getResources().getString(R.string.makemoney_ranking_nodata));
            failedMessageTv.setVisibility(View.VISIBLE);
            makeMoneyIncomeRankRv.setVisibility(View.GONE);
            rootContentNullLl.setVisibility(View.GONE);
        } else if (statusId == 2) {
            failedMessageTv.setText(getActivity().getResources().getString(R.string.makemoney_ranking_nologin));
            failedMessageTv.setVisibility(View.VISIBLE);
            makeMoneyIncomeRankRv.setVisibility(View.GONE);
            rootContentNullLl.setVisibility(View.GONE);
        } else if (statusId == 3) {
            failedMessageTv.setText(getActivity().getResources().getString(R.string.makemoney_ranking_nonet));
            rootContentNullLl.setVisibility(View.VISIBLE);
            failedMessageTv.setVisibility(View.GONE);
            makeMoneyIncomeRankRv.setVisibility(View.GONE);
        }
    }

    private void initListView() {
        adapter = new MakeMoneyRankingAdapter(getActivity(), datas);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        makeMoneyIncomeRankRv.setLayoutManager(linearLayoutManager);
        makeMoneyIncomeRankRv.setAdapter(adapter);
    }

    private void upUiView(YesterdayIncomeRankingResponse data) {
//        if (data == null || data.getRankList()==null || data.getRankList().size()==0) {
//            changeStatusView(1);
//            return;
//        }

        changeStatusView(0);

        List<YesterdayIncomeRankingResponse.MyRankBean> list = new ArrayList<>();
        if (type==1) {
            YesterdayIncomeRankingResponse.MyRankBean myRankBean = data.getMyRank();
            if (myRankBean!=null && myRankBean.getRank()>0) {
                datas.add(new MakeMoneyRankingAdapter.ViewBean(MakeMoneyRankingAdapter.ViewType.TYPE_ITEM_MY, myRankBean));
            }
            if (data.getRankList()!=null)
                list = data.getRankList();
        } else if (type==2) {
            YesterdayIncomeRankingResponse.MyRankBean myRankBean = data.getMyShareRank();
            if (myRankBean!=null && myRankBean.getRank()>0) {
                datas.add(new MakeMoneyRankingAdapter.ViewBean(MakeMoneyRankingAdapter.ViewType.TYPE_ITEM_MY, myRankBean));
            }
            if (data.getShareList()!=null)
                list = data.getShareList();
        } else if (type==3) {
            YesterdayIncomeRankingResponse.MyRankBean myRankBean = data.getMyContributeRank();
            if (myRankBean!=null && myRankBean.getRank()>0) {
                datas.add(new MakeMoneyRankingAdapter.ViewBean(MakeMoneyRankingAdapter.ViewType.TYPE_ITEM_MY, myRankBean));
            }
            if (data.getContributeList()!=null)
                list = data.getContributeList();
        }
        for (int i=0;list!=null && i<list.size();i++) {
            YesterdayIncomeRankingResponse.MyRankBean temp = list.get(i);
            temp.setPosition(i);
            if (i==list.size()-1) {
                temp.setLast(true);
            }
            datas.add(new MakeMoneyRankingAdapter.ViewBean(MakeMoneyRankingAdapter.ViewType.TYPE_ITEM, temp));
        }

        if (datas.size()==0) {
            changeStatusView(1);
        }

        adapter.notifyDataSetChanged();
    }

    public static MakeMoneyRankingFragment newInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        MakeMoneyRankingFragment fragment = new MakeMoneyRankingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.failedMessageTv, R.id.root_content_null_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.root_content_null_ll:
//                if (NetWorkUtils.isNetworkConnected(getActivity())) {
//                    startActivity(new Intent(Settings.ACTION_SETTINGS ));
//                } else {
                    getMoneyRanking();
//                }
                break;
            case R.id.failedMessageTv:
                if (statusId==2) {
                    IntentUtil.getInstance().goLoginActivity(getActivity());
                } else if (statusId==3) {
//                    if (NetWorkUtils.isNetworkConnected(getActivity())) {
//                        startActivity(new Intent(Settings.ACTION_SETTINGS ));
//                    } else {
                        getMoneyRanking();
//                    }
                } else {
                    getMoneyRanking();
                }
                break;
        }
    }
}
